import postList from '../../../public/data/posts-data.js';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic:false
  },
  onColletionTap:function(){
    this.getPostsCollectedAsy();
    //this.setData({collected:!this.data.collected})
  },
  getPostsCollectedAsy:function(){
    let _self = this;
    wx.getStorage({
      key:'posts_collected',
      success:(res)=>{
        //console.log(res.data);
        let postsCollted = res.data,
            currentCollted = postsCollted[_self.data.currentPostId];
        currentCollted =!currentCollted;
        postsCollted[_self.data.currentPostId] = currentCollted;
        //console.log(postsCollted,currentCollted);
        _self.showToast(postsCollted,currentCollted);
      },
      fail:error=>{
        console.log(error);
      }
    })
  },
  showToast:function(list,current){
    console.log(JSON.stringify(list));
    console.log(list);
   wx.setStorageSync("posts_collected",list);
    this.setData({collected:current});
    wx.showToast({
        title: current ? "收藏成功" : "取消成功",
        duration: 1000,
        icon: "success"
    });
  },
  onShareTap:function(){
    let list = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ];
    wx.showActionSheet({
      itemList:list,
      itemColor:"#405f80",
      success:res=>{
        wx.showModal({
          title:list[res.tapIndex],
          content:`确认分享?`,
          success:res=>{
            if(res.confirm){
              wx.showToast({
                title:'分享功能敬请期待...',
                duration:1500,
                icon:'loading'
              })
            }
          }
        })
      }
    });
  },
  onMusicTap:function(){
    // this.setData({isPlayingMusic:!this.data.isPlayingMusic})
    let currentData = postList[this.data.currentPostId];
    let isPlayingMusic = this.data.isPlayingMusic;
    if(isPlayingMusic){
      wx.pauseBackgroundAudio();
      this.setData({isPlayingMusic:false})
      app.globalData.g_isPlayingMusic = false;
    }else {
      wx.playBackgroundAudio({
          dataUrl: currentData.music.url,
          title: currentData.music.title,
          coverImgUrl: currentData.music.coverImg
      });
      this.setData({isPlayingMusic:true});
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_currentMusicPostId = this.data.currentPostId;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let postId = options.id;
    this.data.currentPostId = postId;
    this.setData({postData:postList[postId]});

    let listCollected = wx.getStorageSync('posts_collected');
    if(listCollected){
      this.setData({collected:listCollected[postId]});
    }else {
      listCollected = {};
      listCollected[postId] =false;
      console.log(listCollected);
      wx.setStorageSync('posts_collected',listCollected);
    }

    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId) {
        this.setData({sPlayingMusic: true})
    }
    this.setMusicMonitor();
  },
  setMusicMonitor:function(){
    //点击播放图标和总控开关都会触发这个函数
   var that = this;
   wx.onBackgroundAudioPlay(function (event) {
    //getCurrentPages() 函数用于获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
       var pages = getCurrentPages();
       var currentPage = pages[pages.length - 1];
       console.log(pages,currentPage);
       if (currentPage.data.currentPostId === that.data.currentPostId) {
           // 打开多个post-detail页面后，每个页面不会关闭，只会隐藏。通过页面栈拿到到
           // 当前页面的postid，只处理当前页面的音乐播放。
           if (app.globalData.g_currentMusicPostId == that.data.currentPostId) {
               // 播放当前页面音乐才改变图标
               that.setData({
                   isPlayingMusic: true
               })
           }
           // if(app.globalData.g_currentMusicPostId == that.data.currentPostId )
           // app.globalData.g_currentMusicPostId = that.data.currentPostId;
       }
       app.globalData.g_isPlayingMusic = true;

   });
   wx.onBackgroundAudioPause(function () {
       var pages = getCurrentPages();
       var currentPage = pages[pages.length - 1];
       if (currentPage.data.currentPostId === that.data.currentPostId) {
           if (app.globalData.g_currentMusicPostId == that.data.currentPostId) {
               that.setData({
                   isPlayingMusic: false
               })
           }
       }
       app.globalData.g_isPlayingMusic = false;
       // app.globalData.g_currentMusicPostId = null;
   });
   wx.onBackgroundAudioStop(function () {
       that.setData({
           isPlayingMusic: false
       })
       app.globalData.g_isPlayingMusic = false;
       // app.globalData.g_currentMusicPostId = null;
   });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})